/**
 * XDevice 内部API接口 - 用于libxdevice.c调用底层实现
 * 
 * 这个头文件重新定义底层API函数名，避免与公共API冲突
 */

#ifndef XDEVICE_INTERNAL_H
#define XDEVICE_INTERNAL_H

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/types.h>

#ifdef __cplusplus
extern "C" {
#endif

/* 配置常量 */
#define XDEVICE_MAX_NODES 64
#define XDEVICE_MAX_DEVICES 256
#define XDEVICE_BLOCK_SIZE 4096
#define XDEVICE_WAL_BUFFER_SIZE (64 * 1024 * 1024)  // 64MB
#define XDEVICE_MAX_NODE_ID_LEN 64
#define XDEVICE_MAX_DEVICE_NAME 128
#define XDEVICE_MAX_PATH_LEN 512

/* 内部错误码 */
typedef enum {
    XDEVICE_INTERNAL_OK = 0,
    XDEVICE_INTERNAL_ERROR = -1,
    XDEVICE_INTERNAL_ERROR_INVALID_PARAM = -2,
    XDEVICE_INTERNAL_ERROR_OUT_OF_MEMORY = -3,
    XDEVICE_INTERNAL_ERROR_IO = -4,
    XDEVICE_INTERNAL_ERROR_NETWORK = -5,
    XDEVICE_INTERNAL_ERROR_TIMEOUT = -6,
    XDEVICE_INTERNAL_ERROR_NOT_FOUND = -7,
    XDEVICE_INTERNAL_ERROR_EXISTS = -8,
    XDEVICE_INTERNAL_ERROR_PERMISSION = -9,
    XDEVICE_INTERNAL_ERROR_CONSISTENCY = -10,
    XDEVICE_INTERNAL_ERROR_NODE_DOWN = -11,
    XDEVICE_INTERNAL_ERROR_NOT_INITIALIZED = -12,
    XDEVICE_INTERNAL_ERROR_NO_STORAGE = -13
} xdevice_internal_error_t;

/* 节点状态 */
typedef enum {
    XDEVICE_NODE_UNKNOWN = 0,
    XDEVICE_NODE_LEADER = 1,
    XDEVICE_NODE_FOLLOWER = 2,
    XDEVICE_NODE_CANDIDATE = 3,
    XDEVICE_NODE_DOWN = 4
} xdevice_node_state_t;

/* 存储后端类型 */
typedef enum {
    XDEVICE_STORAGE_LOCAL = 1,         // 本地文件存储
    XDEVICE_STORAGE_NFS = 2,           // NFS网络文件系统
    XDEVICE_STORAGE_NVMEOF = 3,        // NVMe over Fabrics远程存储
    XDEVICE_STORAGE_MOCK = 4           // Mock存储(单元测试)
} xdevice_storage_type_t;

/* 设备状态 */
typedef enum {
    XDEVICE_DEVICE_CREATING = 0,
    XDEVICE_DEVICE_ACTIVE = 1,
    XDEVICE_DEVICE_READONLY = 2,
    XDEVICE_DEVICE_ERROR = 3,
    XDEVICE_DEVICE_DELETED = 4
} xdevice_device_state_t;

/* 前向声明 */
typedef struct xdevice_context xdevice_context_t;
typedef struct xdevice_device xdevice_device_t;

/* 设备信息结构 */
typedef struct {
    char name[XDEVICE_MAX_DEVICE_NAME];
    uint64_t size;
    uint64_t block_count;
    uint32_t block_size;
    xdevice_device_state_t state;
    xdevice_storage_type_t storage_type;
    bool readonly;
    uint64_t created_time;
    uint64_t modified_time;
    uint64_t last_accessed;
} xdevice_device_info_t;

/* 集群信息结构 */
typedef struct {
    int node_count;
    int leader_id;
    uint64_t current_term;
    uint64_t commit_index;
    bool is_healthy;
    char nodes[XDEVICE_MAX_NODES][XDEVICE_MAX_NODE_ID_LEN];
} xdevice_cluster_info_t;

/* 内部API函数声明 - 使用internal_前缀避免冲突 */
xdevice_context_t* internal_xdevice_init(const char* data_dir, int node_id, int cluster_size);
xdevice_context_t* internal_xdevice_init_from_config(const char* config_file);
void internal_xdevice_destroy(xdevice_context_t* ctx);
int internal_xdevice_get_node_id(xdevice_context_t* ctx);
int internal_xdevice_get_cluster_size(xdevice_context_t* ctx);
const char* internal_xdevice_get_data_dir(xdevice_context_t* ctx);
int internal_xdevice_write_status_file(xdevice_context_t* ctx, const char* status_file);
int internal_xdevice_create_device(xdevice_context_t* ctx, const char* name, uint64_t size, xdevice_storage_type_t storage_type);
xdevice_device_t* internal_xdevice_open_device(xdevice_context_t* ctx, const char* name);
void internal_xdevice_close_device(xdevice_device_t* device);
int64_t internal_xdevice_read(xdevice_device_t* device, uint64_t offset, void* buffer, size_t length);
int64_t internal_xdevice_write(xdevice_device_t* device, uint64_t offset, const void* buffer, size_t length);
int internal_xdevice_sync(xdevice_device_t* device);
int internal_xdevice_get_device_info(xdevice_device_t* device, xdevice_device_info_t* info);
int internal_xdevice_get_cluster_info(xdevice_context_t* ctx, xdevice_cluster_info_t* info);
int internal_xdevice_list_devices(xdevice_context_t* ctx, char names[][XDEVICE_MAX_DEVICE_NAME], int max_count);
int internal_xdevice_delete_device(xdevice_context_t* ctx, const char* name);
int internal_xdevice_rename_device(xdevice_context_t* ctx, const char* old_name, const char* new_name);
int internal_xdevice_truncate_device(xdevice_device_t* device, uint64_t size);

#ifdef __cplusplus
}
#endif

#endif /* XDEVICE_INTERNAL_H */
